package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.DTO.AddOrUpdateQuarantimeRegistraionDTO;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.QuarantineRegistrationMapper;
import com.briup.pojo.QuarantineRegistration;
import com.briup.result.PageTemplate;
import com.briup.result.Result;
import com.briup.service.QuarantineRegistrationService;
import com.briup.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author Lenovo
 * @description 针对表【quarantine_registration】的数据库操作Service实现
 * @createDate 2024-03-17 16:55:09
 */
@Service
public class QuarantineRegistrationServiceImpl extends ServiceImpl<QuarantineRegistrationMapper, QuarantineRegistration>
        implements QuarantineRegistrationService {

    @Autowired
    private QuarantineRegistrationMapper quarantineRegistrationMapper;

    @Override
    public PageTemplate qrquery(Integer pageNum, Integer pageSize, String bQualified, String grMechanism) throws SystemException {
        //参数校验
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            //页码或页面大小为空
            throw new SystemException(CodeAndMessageEnum.PAGE_NUMBER_OR_PAGE_SIZE_IS_NULL);
        }
        Page<QuarantineRegistration> QRPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<QuarantineRegistration> QRLambdaQueryWrapper = new LambdaQueryWrapper<>();
        QRLambdaQueryWrapper.eq(Objects.nonNull(bQualified) && StringUtils.hasText(bQualified), QuarantineRegistration::getBQualified, bQualified)
                .like(Objects.nonNull(grMechanism) && StringUtils.hasText(grMechanism), QuarantineRegistration::getGrMechanism, grMechanism);
        quarantineRegistrationMapper.selectPage(QRPage, QRLambdaQueryWrapper);
        PageTemplate pageTemplate = new PageTemplate(QRPage.getTotal(), QRPage.getRecords());
        return pageTemplate;
    }

    @Override
    public Result QRSaveOrUpdate(AddOrUpdateQuarantimeRegistraionDTO addOrUpdateQuarantimeRegistraionDTO) throws SystemException {
        //参数校验
        if (Objects.isNull(addOrUpdateQuarantimeRegistraionDTO.getGrMechanism()) || !StringUtils.hasText(addOrUpdateQuarantimeRegistraionDTO.getGrMechanism())) {
            throw new SystemException(CodeAndMessageEnum.QUARANTINE_REGISTRATION_NAME_IS_NULL);
        }
        //bean拷贝
        QuarantineRegistration quarantineRegistration = BeanCopyUtils.copyBean(addOrUpdateQuarantimeRegistraionDTO, QuarantineRegistration.class);

        if (Objects.isNull(addOrUpdateQuarantimeRegistraionDTO.getGrBatchId())) {
            quarantineRegistrationMapper.insert(quarantineRegistration);
            return Result.success();
        } else {
            quarantineRegistrationMapper.updateById(quarantineRegistration);
            return Result.success();
        }
    }

    @Override
    public Result deleteById(String id) throws SystemException {
        QuarantineRegistration quarantineRegistration = quarantineRegistrationMapper.selectById(id);
        if (Objects.isNull(quarantineRegistration)) {
            throw new SystemException(CodeAndMessageEnum.QUARANTINE_REGISTRATION_IS_NULL);
        }
        quarantineRegistrationMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result deleteByIdAll(List<String> ids) throws SystemException {
        LambdaQueryWrapper<QuarantineRegistration> quarantineRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        quarantineRegistrationLambdaQueryWrapper.in(Objects.nonNull(ids) && !ids.isEmpty(), QuarantineRegistration::getGrBatchId, ids);
        List<QuarantineRegistration> fenceHouses = quarantineRegistrationMapper.selectList(quarantineRegistrationLambdaQueryWrapper);
        if (ids.size() != fenceHouses.size()) {
//            有id找不到对应的信息
            throw new SystemException(CodeAndMessageEnum.QUARANTINE_REGISTRATION_IS_NULL);
        }
        quarantineRegistrationMapper.deleteBatchIds(ids);
        return Result.success();
    }
}




